// 视力记录详情页
const auth = require('../../../utils/auth');
const moment = require('../../../utils/moment');

Page({
  data: {
    id: null,
    loading: true,
    record: null,
    error: null
  },
  onLoad(options) {
    if (options.id) {
      this.setData({ id: options.id });
      this.fetchDetail();
    } else {
      this.setData({ error: '参数错误', loading: false });
    }
  },
  fetchDetail() {
    wx.request({
      url: `${getApp().globalData.apiBaseUrl}/vision/${this.data.id}`,
      method: 'GET',
      header: { 'Authorization': `Bearer ${auth.getToken()}` },
      success: (res) => {
        if (res.statusCode === 200 && res.data && res.data.success) {
          this.setData({ record: res.data.data, loading: false });
        } else if (res.statusCode === 404) {
          this.setData({ error: '记录不存在', loading: false });
        } else {
          this.setData({ error: (res.data && res.data.message) || '加载失败', loading: false });
        }
      },
      fail: () => this.setData({ error: '网络错误', loading: false })
    });
  },
  formatDate(d) { return d ? moment(d).format('YYYY-MM-DD') : ''; }
});
